import { defHttp } from '@/utils/http/axios';
import { GetConnectionListParams, CreateConnectionParams, UpdateConnectionParams, ConnectionModel, GenerateCertificateParams, ConnectionSimpleModel, ValidateCertificateParams, UpdateConnectionStateParams } from './model/connection';
import { BasicFetchResult } from '@/api/model/baseModel'
import { ErrorMessageMode } from '#/axios';
import { UploadFileParams } from '#/axios';
import { AxiosProgressEvent } from 'axios';
import { useGlobSetting } from '@/hooks/setting';

const { apiUrl = '' } = useGlobSetting();

export function getConnectionList(params: GetConnectionListParams, mode: ErrorMessageMode = 'message') {
  return defHttp.get<BasicFetchResult<ConnectionModel>>(
    {
      url: '/connection/connections',
      params,
    },
    {
      errorMessageMode: mode
    }
  );
}

export function getSimpleConnectionList(params: GetConnectionListParams, mode: ErrorMessageMode = 'message') {
  return defHttp.get<BasicFetchResult<ConnectionSimpleModel>>(
    {
      url: '/connection/connections/simples',
      params,
    },
    {
      errorMessageMode: mode
    }
  );
}

export function createConnection(params: CreateConnectionParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post<ConnectionModel>(
    {
      url: `/connection/connections`,
      params
    },
    {
      errorMessageMode: mode,
      successMessageMode: mode,
    }
  );
}

export function updateConnection(id: number, params: UpdateConnectionParams, mode: ErrorMessageMode = 'message') {
  return defHttp.patch<ConnectionModel>(
    {
      url: `/connection/connections/{id}`,
      params
    },
    {
      errorMessageMode: mode,
      successMessageMode: mode,
    }
  );
}

export function changeConnectionState(id: number|string, params: UpdateConnectionStateParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post<ConnectionModel>(
    {
      url: `/connection/connections/${id}/state`,
      params,
    },
    {
      errorMessageMode: mode,
      successMessageMode: mode,
    }
  );
}

export function generateCertificate(id: number|string, params: GenerateCertificateParams, mode: ErrorMessageMode = 'message') {
  return defHttp.post(
    {
      url: `/connection/connections/${id}/certificates/generate`,
      params
    },
    {
      errorMessageMode: mode,
      successMessageMode: mode,
    }
  );
}

export function uploadCertificate(id: number|string, params: UploadFileParams, onUploadProgress: (progressEvent: AxiosProgressEvent) => void,) {
  return defHttp.uploadFile(
    {
      url: `${apiUrl}/connection/connections/${id}/certificates/upload`,
      onUploadProgress,
    },
    params,
  );
}

export function validateCertificate(certificateId: number, mode: ErrorMessageMode = 'message') {
  const params: ValidateCertificateParams = {
    certificate_id: certificateId,
  }
  return defHttp.post(
    {
      url: `/connection/connections/certificates/validate`,
      params,
    },
    {
      errorMessageMode: mode,
      successMessageMode: mode,
    }
  );
}

export function getCertificateFilings(certificateId: number, mode: ErrorMessageMode = 'message') {
  const params = {
    certificate_id: certificateId,
  }
  return defHttp.get(
    {
      url: `/connection/connections/certificates/filings`,
      params,
    },
    {
      errorMessageMode: mode,
    }
  );
}

export function getCertificateServer(mode: ErrorMessageMode = 'message') {
  return defHttp.get(
    {
      url: `/connection/connections/certificates/server`,
    },
    {
      errorMessageMode: mode,
    }
  );
}